home *** CD-ROM | disk | FTP | other *** search
- /*
- ICeTEe.c
-
- C translation of the ICeTEe.p pascal source file.
- */
-
- #include <Processes.h>
- #include <LowMem.h>
- #include <Notification.h>
- #include <Traps.h>
- #include <GestaltEqu.h>
- #include <OSUtils.h>
- #include <Types.h>
- #include <TextEdit.h>
-
- #if defined(powerc) || defined (__powerc)
- #include <stdlib.h>
- #else
- #include <SetUpA4.h>
- #endif
-
- #include "ICeTEe.h"
- #include "ShowInitIcon.h"
-
- #include "ICTypes.h"
- #include "IC Component API.h"
-
- static icteGlobalsPtr gGlobalsPtr;
-
- #define SetMyGlobals(globs) (gGlobalsPtr=((icteGlobalsPtr)(globs)))
- #define GetMyGlobals() (gGlobalsPtr)
-
- static pascal OSErr MyGestalt(OSType selector,long* response){
-
- *response=(long)GetMyGlobals();
- return noErr;
- }
-
- static void CallTEClick(Point pt,Boolean fExtend,TEHandle teh,TEClickUPP tec){
-
- CallTEClickProc(tec,pt,fExtend,teh);
- }
-
- static pascal void MyNMResponseProc(NMRecPtr nm){
- THz ozone;
- Handle strh;
- OSErr junk;
-
- junk=NMRemove(nm);
- ozone=GetZone();
- SetZone(SystemZone());
-
- strh=RecoverHandle((Ptr)nm->nmStr);
-
- if (strh!=(Handle)0){
- DisposeHandle(strh);
- }
-
- DisposePtr((Ptr)nm);
- SetZone(ozone);
-
- }
-
- static Boolean CurrentProcessExcluded(void){
- ProcessSerialNumber PSN;
- ProcessInfoRec info;
- exHandle exclusions;
- short i;
- icteGlobalsPtr globals=GetMyGlobals();
-
- PSN.highLongOfPSN=0;
- PSN.lowLongOfPSN=kCurrentProcess;
- info.processInfoLength=sizeof(ProcessInfoRec);
- info.processName=(StringPtr)0;
- info.processAppSpec=(FSSpecPtr)0;
-
- if (GetProcessInformation(&PSN,&info)==noErr){
- exclusions=globals->exclusions;
- for (i=0;i<((GetHandleSize((Handle)exclusions))/4);i++){
- if ((*exclusions)[i]==info.processSignature)
- return true;
- }
- }
-
- return false;
- }
-
- static Boolean HaveComponentManager(void){
- long resp;
-
- return (Gestalt(gestaltComponentMgr,&resp)==noErr);
- }
-
- static ICError DoCommandClick(TEHandle teh,long selStart,long selEnd){
- ComponentInstance inst;
- ICError err;
- ICError err2;
- Handle text;
- SignedByte s;
- RgnHandle rgn;
- short i;
- long junklong;
- Str31 hint="\pmailto",at="\p@";
- Handle urlh;
-
- if (HaveComponentManager()){
- err=ICCStart(&inst,kCreator);
- } else {
- err=noCMErr;
- }
- if (err==noErr){
- err=ICCFindConfigFile(inst,0,nil);
- if (err==noErr){
- text=(Handle)TEGetText(teh);
- s=HGetState(text);
- HLock(text);
- urlh=NewHandle(0);
-
- err=ICCParseURL(inst,hint,*text,GetHandleSize(text),&selStart,&selEnd,urlh);
-
- if (err==noErr){
- hint[0]=0;
-
- if (Munger(urlh,0,&at[1],at[0],nil,0)>=0){
- hint[0]=6;
- hint[1]='m';hint[2]='a';hint[3]='i';
- hint[4]='l';hint[5]='t';hint[6]='o';
- }
-
- err=ICCLaunchURL(inst,hint,*text,GetHandleSize(text),&selStart,&selEnd);
- }
- DisposeHandle(urlh);
- TESetSelect(selStart,selEnd,teh);
-
- if (err==noErr){
- for (i=0;i<LMGetMenuFlash();i++){
- Delay(5,&junklong);
- TEDeactivate(teh);
- Delay(5,&junklong);
- TEActivate(teh);
- }
- }
-
- HSetState(text,s);
- }
- err2=ICCStop(inst);
- if (err==noErr)
- err=err2;
- }
-
- return err;
- }
-
- static void GetIndStrH(Handle h,short index,StringPtr str){
- short count,i;
- long ps;
- short ** sh=(short**)h;
- unsigned char *ucp,uc;
-
- count=**sh;
-
- if ((1<=index)&&(index<=count)){
- ps=(long)sizeof(short);
- ucp=(unsigned char*)(*h);
-
- for (i=0;i<index;i++){
- ucp=(unsigned char*)((*h)+ps);
- ps += *ucp +1;
- }
-
- ucp=(unsigned char*)((*h)+ps);
-
- BlockMove((Ptr)((*h)+ps),str,*ucp+1);
- } else {
- str[0]=0;
- }
- }
-
- static void MyTEClick(TEHandle teh,long old_selStart,long old_selEnd){
- ICError err;
- Str255 message;
- Str255 msg;
- NMRecPtr nm;
- short strindex;
- StringHandle strh;
- icteGlobalsPtr globals=GetMyGlobals();
- unsigned char* cp,* mcp;
-
- if (!CurrentProcessExcluded()){
- if (!((old_selStart<=(*teh)->selStart)&&((*teh)->selStart<=old_selEnd)
- &&(old_selStart<=(*teh)->selEnd)&&((*teh)->selEnd<=old_selEnd))){
- old_selStart=(*teh)->selStart;
- old_selEnd=(*teh)->selEnd;
- }
-
- err=DoCommandClick(teh,old_selStart,old_selEnd);
-
- if (err!=noErr){
- switch(err){
- case badComponentInstance:
- strindex=strNoICErr;
- break;
- case noCMErr:
- strindex=strNoCMErr;
- break;
- case badComponentSelector:
- strindex=strInsufficientICErr;
- break;
- case memFullErr:
- strindex=strNoMemoryErr;
- break;
- case afpItemNotFound:
- strindex=strCantFindHelperErr;
- break;
- case icPrefNotFoundErr:
- strindex=strNoHelperErr;
- break;
- case icNoURLErr:
- strindex=strNoURLErr;
- break;
- case noPortErr:
- strindex=strCantHackIt;
- break;
- default:
- strindex=strMiscErr;
- break;
- }
-
- GetIndStrH(globals->errors,strindex,message);
-
- if (message[0]!=0){
- NMUPP respUpp;
-
- if (strindex==strMiscErr){
- // replace the ^0 with the error
- NumToString(err,msg);
-
- cp=message+1;
- while (*cp!='^')
- cp++;
-
- // cp points to the ^
- cp++;cp++;
- mcp=msg+1;
-
- message[0]=message[0]-2+msg[0]; // add the new string len
- // points at the close paren...
- while (msg[0]){
- *cp=*mcp;
- cp++;
- mcp++;
- msg[0]--;
- }
-
- // now add the ).
- *cp=')';cp++;
- *cp='.';
- }
-
- strh=NewString(message);
-
- HLock((Handle)strh);
- nm=(NMRecPtr)NewPtrSysClear(sizeof(NMRec));
-
- respUpp=NewNMProc(MyNMResponseProc);
-
- if (nm!=(NMRecPtr)0){
- nm->qType=nmType;
- nm->nmMark=0;
- nm->nmIcon=(Handle)0;
- nm->nmSound=(Handle)0;
- nm->nmStr=*strh;
- nm->nmResp=respUpp;
-
- err=NMInstall(nm);
- } else {
- SysBeep(10);
- }
- }
- }
- }
- }
-
- static Boolean IsKeyPressed(unsigned short k){
- unsigned char km[16];
-
- GetKeys((long*)km);
-
- return ((km[k>>3]>>(k&7))&1);
- }
-
- static pascal void PascalTEClickPatch(Point pt,Boolean fExtend,TEHandle teh){
- long old_selStart,old_selEnd;
- icteGlobalsPtr globals;
- THz ozone;
- Boolean command_key;
- long savea4;
-
- // save all of the registers
- #if defined(powerc) || defined (__powerc)
- __rsrcinit();
- #else
- asm {
- movem.l a0-a6/d0-d6,-(sp)
- }
- SetUpA4();
- #endif
-
- globals=GetMyGlobals();
- old_selStart=(*teh)->selStart;
- old_selEnd=(*teh)->selEnd;
-
- command_key=IsKeyPressed(55);
-
- CallTEClick(pt,fExtend,teh,globals->old_teclick);
-
- if (command_key){
- ozone=GetZone();
- SetZone(SystemZone());
-
- MyTEClick(teh,old_selStart,old_selEnd);
-
- SetZone(ozone);
- }
-
- // restore all of the registers
- #if defined(powerc) || defined (__powerc)
- __rsrcterm();
- #else
- RestoreA4();
- asm {
- movem.l (sp)+,a0-a6/d0-d6
- }
- #endif
-
- }
-
- void main(void){
- Ptr ts;
- Handle res;
- THz ozone;
- OSErr err,err2;
- long response;
- icteGlobalsPtr globals;
- Handle exclusions;
- Handle errors;
- VersRecHndl vers;
-
- #if defined(powerc) || defined (__powerc)
- /* PPC C way to handle an INIT */
- __rsrcinit();
-
- ts=(Ptr)PascalTEClickPatch;
-
- #else
- /* Standard C/68k way to handle an INIT */
-
- asm {
- move.l a0,ts
- }
-
- RememberA0();
- SetUpA4();
-
- #endif
-
- // detach our resource
-
- if (ts!=(Ptr)0){
- res=RecoverHandle(ts);
-
- err=noErr;
-
- if (res!=(Handle)0){
- DetachResource(res);
- } else {
- // RecoverHandle failed.
- err=MemError();
- }
-
- ShowInitIcon(rICTEIcon,false);
-
- ozone=GetZone();
- SetZone(SystemZone());
-
- // err=noErr;
-
- if ((Gestalt(gestaltSystemVersion,&response)!=noErr)||(response<0x700)){
- err=unimpErr;
- }
-
- if (err==noErr){
- globals=(icteGlobalsPtr)NewPtrSysClear(sizeof(icteGlobals));
- if (globals==(icteGlobalsPtr)0)
- err=MemError();
- }
-
- if (err==noErr){
- SetMyGlobals(globals);
- globals=GetMyGlobals();
-
- // init globals
- globals->signature=kCreator;
-
- vers=(VersRecHndl)Get1Resource('vers',1);
- if (vers!=(VersRecHndl)0){
- HLock((Handle)vers);
- globals->version=(*vers)->numericVersion;
- HUnlock((Handle)vers);
- }
-
- exclusions=Get1Resource('EXCL',rExclusions);
- err=HandToHand(&exclusions);
-
- globals->exclusions=(exHandle)exclusions;
-
- errors=Get1Resource('STR#',rErrorStrings);
- err2=HandToHand(&errors);
-
- globals->errors=errors;
-
- if (err==noErr)
- err=err2;
- }
-
- if (err==noErr){
- SelectorFunctionUPP sel;
-
- sel=NewSelectorFunctionProc(MyGestalt);
-
- err=NewGestalt(kCreator,sel);
- }
-
- if (err==noErr){
- // install the patch
- UniversalProcPtr nteclick;
-
- globals->old_teclick=(TEClickUPP)NGetTrapAddress(_TEClick,ToolTrap);
-
- nteclick=(UniversalProcPtr)NewTEClickProc(PascalTEClickPatch);
-
- NSetTrapAddress(nteclick,_TEClick,ToolTrap);
- }
-
- /*
- If we got an error then we bleed memory all over the place, this is not
- an accident. How many copies of the init can you reasonably fail to install?
- */
-
- SetZone(ozone);
-
- if (err==noErr){
- ShowInitIcon(rICTEIcon,true);
- } else {
- ShowInitIcon(rFailedIcon,true);
- }
- }
- #if defined(powerc) || defined (__powerc)
- __rsrcterm();
-
- #else
- RestoreA4();
- #endif
- }
-
-